package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.bussiness.rank.CrossGeneralActivityRankBusiness;
import com.yanqu.road.logic.bussiness.rank.CrossStrongholdActivityRankBusiness;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.sort.CrossRankSort;
import com.yanqu.road.pb.rank.RankProto;

import java.math.BigInteger;
import java.util.LinkedList;

public class CrossGeneralActivityUserRankListModel {

    private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

    private CrossRankSort crossRankSort;

    private boolean isNeedSync = false;

    private boolean isUpdate = false;

    private boolean needUpdateDB;           //是否需要把数据入库

    private LinkedList<CrossUserRank> rankList;

    private int activityId;

    private int conditionType;

    private RankProto.RankListMsg.Builder rankListMsg;

    public CrossGeneralActivityUserRankListModel(int activityId, boolean needUpdateDB) {
        crossRankSort = new CrossRankSort();
        rankList = new LinkedList<>();
        rankListMsg = RankProto.RankListMsg.newBuilder();
        this.needUpdateDB = needUpdateDB;
        this.activityId = activityId;
    }

    public int getActivityId() {
        return activityId;
    }

    public int getConditionType() {
        return conditionType;
    }

    public void setConditionType(int conditionType) {
        this.conditionType = conditionType;
    }

    public boolean isNeedSync() {
        return isNeedSync;
    }

    public void setNeedSync(boolean needSync) {
        isNeedSync = needSync;
    }

    public RankProto.RankListMsg.Builder getRankListMsg() {
        return rankListMsg;
    }

    public LinkedList<CrossUserRank> getRankList() {
        return rankList;
    }

    public void setRankList(LinkedList<CrossUserRank> userRankList) {
        synchronized (this) {
            rankList = new LinkedList<>(userRankList);
            RankListHelper.checkUserRankListValid(rankList);       //校验一下数组
            rankList.sort(crossRankSort);
            while (rankList.size() > rankNum){
                rankList.removeLast();
            }
            initRankMsg();
        }
    }

    private void initRankMsg() {
        rankListMsg = CrossRankPb.parseRankListMsg(rankList);
    }

    public int getMyRank(long userId, long serverId) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(CrossUserRank userRank : rankList){
                if(userRank.getUserId() == userId && userRank.getUserBaseInfo().getServerId() == serverId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        if(rank > rankNum){
            rank = -1;
        }
        return rank;
    }

    public CrossUserRank getMyRankInfo(long userId, long serverId) {
        synchronized (this) {
            for(CrossUserRank userRank : rankList){
                if(userRank.getUserId() == userId && userRank.getUserBaseInfo().getServerId() == serverId){
                    return userRank;
                }
            }
        }
        return null;
    }

    /**
     * 添加到排行
     */
    public void rankChange(CrossUserRank crossUserRank) {
        boolean isChange = false;
        synchronized (this) {
            int size = rankList.size();
            CrossUserRank userRank = getMyRankInfo(crossUserRank.getUserId(), crossUserRank.getUserBaseInfo().getServerId());
            if(null != userRank){
                userRank.setUserBaseInfo(crossUserRank.getUserBaseInfo());
                if(0 != userRank.getValue().compareTo(crossUserRank.getValue())){
                    userRank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                }
                userRank.setValue(crossUserRank.getValue());
                isChange = true;
            }else {
                if (size < rankNum) {
                    CrossUserRank rank = crossUserRank.clone();
                    rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                    rankList.add(rank);
                    isChange = true;
                } else if (rankList.getLast().getValue().compareTo(crossUserRank.getValue()) < 0) {
                    CrossUserRank rank = crossUserRank.clone();
                    rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                    rankList.removeLast();
                    rankList.add(rank);
                    isChange = true;
                }
            }
            if(isChange) {
                rankList.sort(crossRankSort);
                initRankMsg();
                isNeedSync = true;
                isUpdate = true;
            }
        }
    }

    public BigInteger getMinRankScore() {
        synchronized (this) {
            if (rankList.size() > 0 && rankList.size() >= rankNum) {
                return rankList.getLast().getValue();
            }
            return BigInteger.ZERO;
        }
    }

    public void setRankNum(int rankNum) {
        this.rankNum = rankNum;
    }

    public boolean isUpdate() {
        return isUpdate;
    }

    public void setUpdate(boolean update) {
        isUpdate = update;
    }

    public void save() {
        if(needUpdateDB) {
            LinkedList<CrossUserRank> newRankList;
            synchronized (this) {
                if (!isUpdate) {
                    return;
                }
                newRankList = new LinkedList<>(rankList);
                isUpdate = false;
            }
            CrossGeneralActivityRankBusiness.updateCrossActivityNewRankList(newRankList, conditionType);
        }
    }
}
